package morningcat.demo1.hello;

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;
import morningcat.MQParams;

/**
 * XXX
 *
 * @author mengzhang6
 * @date 2018/12/17 9:39
 */
public class Sender {

    public static void main(String[] argv) throws Exception {
        // ConnectionFactory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost(MQParams.SERVER_HOST);
        factory.setPort(MQParams.SERVER_PORT);

        // try-with-resources statement
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {

            // 信道
            channel.queueDeclare(MQParams.QUEUE_NAME_HELLO, false, false, false, null);

            // To Send
            String message = "你好 RabbitMQ , timestamp:" + System.currentTimeMillis();
            channel.basicPublish("", MQParams.QUEUE_NAME_HELLO, null, message.getBytes("UTF-8"));
            System.out.println(" [x] Sent '" + message + "'");
        }
    }
}
